package com.zhdl.modules.eastdosing;

import com.zhdl.hardware.io.smartLink.repository.impl.IOCmdHandleDaoImpl;
import com.zhdl.hardware.meich_camera.MeichCameraCmdHandleDao;
import com.zhdl.hardware.robot.borunte.repository.impl.BorunteCmdHandleDaoImpl;
import com.zhdl.modules.autopick.service.RobotActionService;
import com.zhdl.modules.tools.CommonCallback;
import com.zhdl.network.asclltcp.AsciiAfterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
@Slf4j
@Component
public class NewCameraProcess {
    @Resource
    MeichCameraCmdHandleDao meichCameraCmdHandleDao;
    @Resource
    private RobotActionService robotActionService;
    @Resource
    private BorunteCmdHandleDaoImpl borunteCmdHandleDao;
    @Resource
    private IOCmdHandleDaoImpl ioCmdHandleDao;
    int reTryCount = 1;

    private final int[] pickLocation =  new  int[3];
    public void  init(){
        borunteCmdHandleDao.init("192.168.4.4:502");
        ioCmdHandleDao.init("192.168.1.108:502");
        meichCameraCmdHandleDao.init();
    }
    public void startCamera(){
        meichCameraCmdHandleDao.startCamera(s -> {
            s = AsciiAfterService.hexToAscii(s);
            s = s.trim();
            log.info("相机识别结果"+s);
            String[] split = s.split(",");
            if (!split[0].equals("1")) {
                log.info("相机识别结果"+s+">>>>split[0]"+split[0]);
                if (split[0].equals("4")){
                    log.info("无料...");
                }else if (split[0].equals("5")){
                    reTryCount++;
                    if (reTryCount>=5){
                        log.info("重试四次后仍然无结果..需要人工介入!!!!!!!");
                    }
                    log.info("无结果...需要重试"+reTryCount);
                    startCamera();
                }
                return;
            }
            reTryCount = 1;
            for (int i = 0; i < 3; i++) {
                pickLocation[i] = (int) (Float.parseFloat(split[i+1]) * 1000);
                if (i==2){
                    pickLocation[i]+=400*1000;
                }
            }
            BorunteCmdHandleDaoImpl.UVW[2] = (int) (Float.parseFloat(split[6]) * 1000);
            borunteCmdHandleDao.borunteSetPickPoint(pickLocation);
            borunteCmdHandleDao.borunteSetPickZ(pickLocation);
            borunteCmdHandleDao.borunteWriteSwitch(160, true);
        },reTryCount);
    }
}
